import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/components/flash_sale_product_item.dart';
import 'package:sinosheets/components/normal_product_item.dart';
import 'package:sinosheets/components/no_data_widget.dart';
import 'package:sinosheets/r.dart';

import 'logic.dart';

class SearchProductPage extends StatefulWidget {
  const SearchProductPage({Key? key}) : super(key: key);

  @override
  State<SearchProductPage> createState() => _SearchProductPageState();
}

class _SearchProductPageState extends State<SearchProductPage> {
  final SearchProductLogic logic = Get.put(SearchProductLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchProductLogic>(builder: (get) {
      return Scaffold(
        appBar: AppBar(
          title: Image.asset(
            R.assetsImgSinosheets,
            width: 100.r,
            fit: BoxFit.fill,
          ),
          centerTitle: true,
        ),
        body: Padding(
          padding: EdgeInsets.symmetric(horizontal: 10.r, vertical: 10.r),
          child: get.searchProductList.isEmpty
              ? NoDataWidget.noSearchResult(
                  title: 'No Search Results',
                  description: 'No products found matching your search criteria. Try different keywords or browse categories.',
                  showButton: true,
                  buttonText: 'Back to Home',
                  onButtonTap: () {
                    Get.back();
                  },
                )
              : GridView.builder(
                  shrinkWrap: true,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 16.r,
                    crossAxisSpacing: 16.r,
                    childAspectRatio: 0.59,
                  ),
                  itemCount: get.searchProductList.length,
                  itemBuilder: (context, index) {
                    //判断mallProduct是否为空 空是普通商品 不是空是零元购商品
                    if (get.searchProductList[index]['mallProduct'] != null) {
                      return FlashSaleProductItem(item: get.searchProductList[index]['mallProduct']);
                    } else {
                      return NormalProductItem(item: get.searchProductList[index]);
                    }
                  },
                ),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<SearchProductLogic>();
    super.dispose();
  }
}
